import {login, logOut, verify} from '@/api/user_api'

export default {
    namespaced: true,
    state: {
        user: null,
        isLogin: false
    },
    mutations: {
        setUser(state, user) {
            state.user = user
        },
        setIsLogin(state, isLogin) {
            state.isLogin = isLogin
        }
    },
    actions: {
        async verify(context) {
            context.commit("setIsLogin", true);
            // 调用service里面的verify方法
            let resp = await verify()
            if (resp != null) {
                context.commit("setUser", resp.data);
            }
            context.commit("setIsLogin", false);
        },
        async login(context, payload) {
            let flag = false;
            context.commit("setIsLogin", true);
            let resp = await login(payload);
            context.commit("setUser", resp.data);
            context.commit("setIsLogin", false);

            // 如果code = 0 ,则返回ture
            if (resp.code === 0) {
                flag = true;
            }
            return flag;
        },
        logOut(context) {
            logOut();
            // 将仓库里的user设置为null
            context.commit("setUser", null);
        }
    }
}